package com.guigu.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.StorageGatherDetailsDto;
import com.guigu.erp.mapper.StorageCellMapper;
import com.guigu.erp.mapper.StorageGatherDetailsMapper;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.pojo.StorageGatherDetails;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.service.StorageGatherDetailsService;
import com.guigu.erp.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Lenovo
 */
@Service
public class StorageGatherDetailsServiceImpl extends ServiceImpl<StorageGatherDetailsMapper, StorageGatherDetails>
        implements StorageGatherDetailsService {
    @Autowired
    private StorageGatherDetailsMapper storageGatherDetailsMapper;
    @Autowired
    private StorageGatherDetailsService storageGatherDetailsService;
    @Autowired
    private StorageCellService storageCellService;
    @Autowired
    private StorageCellMapper storageCellMapper;

    @Override
    public PageUtils<StorageGatherDetails> gatherDetailsPage(Integer id, int pageNo, int pageSize) {
        QueryWrapper wrapper=new QueryWrapper();
        //额外加1个条件
        //父级id
        System.out.println("id:"+id);
        wrapper.eq("parent_id",id);
        PageHelper.startPage(pageNo,pageSize);
        List<StorageGatherDetails> storageGatherDetails=this.list(wrapper);
        PageInfo<StorageGatherDetails> pageInfo=new PageInfo<StorageGatherDetails>(storageGatherDetails);

        //把PageHelper PageUtils
        PageUtils<StorageGatherDetails> pageUtils=new PageUtils<StorageGatherDetails>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        System.out.println(pageUtils);
        return pageUtils;
    }

    @Override
    public StorageGatherDetailsDto listByParentId(String productId) {
        StorageGatherDetailsDto storageGatherDto=storageGatherDetailsMapper.listByParentId(productId);
        System.out.println("之前"+storageGatherDto);
        return storageGatherDto;
    }

    @Override
    public List<StorageGatherDetails> detailsById(int id) {
        List<StorageGatherDetails> details=storageGatherDetailsMapper.detailsById(id);
        return details;
    }

    @Override
    public void updateDto(StorageGatherDetailsDto storageGatherDetailsDto) {
        int id = storageGatherDetailsDto.getId();
        //获取要修改的 本次入库数量
        BigDecimal beInAmount= storageGatherDetailsDto.getGatheredAmount();
        System.out.println(id+"aa");

        QueryWrapper<StorageGatherDetails> wrapper = new QueryWrapper<StorageGatherDetails>();
        wrapper.eq("id", id);
        StorageGatherDetails storageGatherDetails = storageGatherDetailsService.getOne(wrapper);

        storageGatherDetails.setGatherTag("K002-2");
        storageGatherDetails.setGatheredAmount(beInAmount);
        storageGatherDetailsService.updateById(storageGatherDetails);
        //修改库存单元的当前储存量
        QueryWrapper<StorageGatherDetails> wrapper1=new QueryWrapper<StorageGatherDetails>();
        wrapper1.eq("id",id);
        StorageGatherDetails storageGatherDetails1=storageGatherDetailsService.getOne(wrapper1);
        //取出产品编号，用产品编号修改当前储存量
        String productId= storageGatherDetails1.getProductId();
        QueryWrapper<StorageCell> wrapper2=new QueryWrapper<StorageCell>();
        wrapper2.eq("product_id",productId);
        StorageCell storageCell=storageCellService.getOne(wrapper2);
        //取出dto里已经计算好的当前储存量
        int amount=storageGatherDetailsDto.getStorageCell().getAmount();
        storageCell.setAmount(amount);
        storageCellMapper.updateAmount(storageCell);
    }
}
